package com.kasax.test.bc.model.netapi

import com.kasax.hilog.Logger
import com.kasax.test.bc.model.BaseModel
import com.kasax.utils.GsonUtils
import com.kasax.utils.NetHelper.okHttpClient
import okhttp3.Call
import okhttp3.FormBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import okio.IOException

class OKHttpClientModel : BaseModel() {

    override fun testGetIpAddr() {
        val request = Request.Builder().url("https://cx.shouji.360.cn/phonearea.php?number=17688888888").get().build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
                Logger.log("testGetIpAddr error: ${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                Logger.log("testGetIpAddr responseCode: ${response.code} , response: ${GsonUtils.deEnconde(response.body?.string())}")
            }
        })
    }

    override fun getToabaiIp() {
        val requestBody =
            FormBody.Builder().addEncoded("accessKey", "alibaba-inc").addEncoded("ip", "11.11.11.11").build()
        val request = Request.Builder().url("https://ip.taobao.com/outGetIpInfo").post(requestBody).build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
                Logger.log("getToabaiIp error: ${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                Logger.log("getToabaiIp responseCode: ${response.code} , response: ${response.body?.string()}")
            }
        })
    }

    override fun getFormUrl() {
        val requestBody = FormBody.Builder().addEncoded("param", "测试22").addEncoded("email", "crap.cn@gmail.com")
            .addEncoded("555", "我").build()
        val request = Request.Builder().url("http://api.crap.cn/visitor/example/post.do").post(requestBody).build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
                Logger.log("getFormUrl error: ${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                Logger.log("getFormUrl responseCode: ${response.code} , response: ${response.body?.string()}")
            }
        })
    }

    override fun getJsonParam() {
        val requestBody = RequestBody.create(
            "application/json".toMediaTypeOrNull(),
            "{\n" + "\"id\":\"8989-dddvdg\",\n" + "\"name\":\"文章标题-JSON格式参数演示\",\n" + "\"brief\":\"快速入门json参数\",\n" + "\"category\":\"分类\"\n" + "}"
        )
        val request = Request.Builder().url("http://api.crap.cn/visitor/example/json.do").post(requestBody).build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
                Logger.log("getJsonParam error: ${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                Logger.log("getJsonParam responseCode: ${response.code} , response: ${response.body?.string()}")
            }
        })
    }

    override fun getXmlParam() {
        val requestBody = RequestBody.create(
            "application/xml".toMediaTypeOrNull(),
            "<users>\n" + "<name>用户名</name>\n" + "<value>value值</value>\n" + "</users>"
        )
        val request = Request.Builder().url("http://api.crap.cn/visitor/example/xml.do").post(requestBody).build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
                Logger.log("getXmlParam error: ${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                Logger.log("getXmlParam responseCode: ${response.code} , response: ${response.body?.string()}")
            }
        })
    }

    override fun testWanAndroidBanners() {
        val request = Request.Builder().url("https://www.wanandroid.com/banner/json").get().build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
                Logger.log("error: ${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                Logger.log("responseCode: ${response.code} , response: ${response.body?.string()}")
            }
        })
    }

    override fun testWanAndroidLogin() {
        val requestBody =
            FormBody.Builder().addEncoded("username", "zzgtest02").addEncoded("password", "123456").build()
        val request = Request.Builder().url("https://www.wanandroid.com/user/login").post(requestBody).build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: Call, e: IOException) {
                Logger.log("testWanAndroidLogin error: ${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                Logger.log(
                    "testWanAndroidLogin responseCode: ${response.code} , response: ${
                        GsonUtils.deEnconde(
                            response.body?.string()
                        )
                    }"
                )
            }
        })
    }
}